﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using Utility;
using Model.TransfersConfig;
using BLL.Action.Core.Macro;



namespace BLL.Parser
{
    public class TransfersParser
    {
        XmlReadWriteHelper helper_ = null;


        public TransfersTask GetTaskItem(string Path)
        {
            TransfersTask task = new TransfersTask();
            try
            {
                //logHelper.GetMainLogger().InfoFormat("开始解析{0}文件", Path);
                //Path = @"TransfersTimeConfig.xml";
                helper_ = new XmlReadWriteHelper(Path);



                XmlNode node = helper_.GetNode(@"//Transfers/HotelInfos");

                //task.TriggerTime =Convert.ToDateTime(node.Attributes.GetNamedItem("TriggerTime").Value);

                XmlNodeList NodeList = node.ChildNodes;



                foreach (XmlNode hotels in NodeList)
                {
                    XmlNodeList hotelList = hotels.ChildNodes;

                    Hotelinfo hotel = new Hotelinfo();
                    hotel.DateInterFace = hotelList[1].InnerText;
                    hotel.Hotelid = hotelList[0].InnerText;
                    hotel.TriggerTime = Convert.ToDateTime(hotelList[2].InnerText);

                    hotel.Tables = GetTaskTables(hotelList[3].ChildNodes);
                    if (task.Hotels == null)
                        task.Hotels = new List<Hotelinfo>();
                    task.Hotels.Add(hotel);
                }
            }
            catch {
                //AppToolKit.Core.Logger.LoggerHelper.GetCallingMethod();

                //logHelper.GetErrorLogger().ErrorFormat("错误:{0}", ex.Message);
            }
            return task;
        }
        private List<TaskTable> GetTaskTables(XmlNodeList nodes)
        {
            List<TaskTable> list = new List<TaskTable>();

            foreach (XmlNode table in nodes)
            {
                TaskTable tableInfo = new TaskTable();
                tableInfo.HotelTableName = table["HotelTableName"].InnerText;
                tableInfo.SqlCondition = ReplaceMacro(table["SqlCondition"].InnerText); 
                tableInfo.TablePrimaryKey = table["PrimaryKey"].InnerText.Split(','); 
                tableInfo.DateSource = table["DataSource"].InnerText; 
                tableInfo.CenterTableName = table["CenterTableName"].InnerText;
                if (!string.IsNullOrEmpty(table["EntityName"].InnerText))
                tableInfo.Entity = Type.GetType(table["EntityName"].InnerText);

                tableInfo.Descript = table["Descript"].InnerText;
                tableInfo.RequireTransfers = table["RequireTransfers"].InnerText == "T" ? true : false;

                list.Add(tableInfo);
            }

            return list;
        }
        public string ReplaceMacro(string str)
        {
           return  TransfersMacro.GetMacro(str);                 
        }
    }
}
